/*
 * @Author: wangshuixin 2412658453@qq.com
 * @Date: 2024-12-17 12:40:24
 * @LastEditors: wangshuixin 2412658453@qq.com
 * @LastEditTime: 2024-12-27 09:03:14
 * @FilePath: \tspods\utils\mathematics.ts
 * @Description: 运算方法
 */
import type { ExpressionTerm, VariableTermElement } from '@/types/equation'

/**
 * @function 运算项中是否存在字母
 * 
*/
export const mathematicsTermExistContainChars = (mathematicsTerm: ExpressionTerm) => {
  let existChar = false
  if (mathematicsTerm.constant) {
    mathematicsTerm.constant.forEach((subTerm: VariableTermElement) => {
      if (subTerm.text && stringContainChars(subTerm.text)) {
        existChar = true
      }
    })
    return existChar
  } else {
    throw(`运算项：${JSON.stringify(mathematicsTerm)}，其常数项不存在！`)
  }
}

/**
 * @function 加法
 * 
 * @param {ExpressionTerm} previous 前一个加数因子
 * @param {ExpressionTerm} next 后一个加数因子
 * @abstract 因子中type 不存在 或 undefined 默认为 “+” 加号
*/
export const mathematics_addition = (previous: ExpressionTerm, next: ExpressionTerm): ExpressionTerm => {
  if (!mathematicsTermExistContainChars(previous) && !mathematicsTermExistContainChars(next)) {
    // 此处算法需优化 >> 纯数字
    let previousConstant = previous.constant && previous.constant[0] && previous.constant[0].text ? previous.constant[0].text : 0
    let nextConstant = 0
    if (next.constant) {
      next.constant.forEach((subTerm: VariableTermElement) => {
        const waitConstant = subTerm.text ?? '0'
        nextConstant = nextConstant + Number(waitConstant) * (subTerm.type === '2' ? -1 : 1)
      })
    } else {
      throw('进行加法运算的项，其参数类型错误！')
    }
    
    let result = Number(previousConstant) * (previous.type === '2' ? -1 : 1) + Number(nextConstant) * (next.type === '2' ? -1 : 1)
    let resultTerm = {
      constant: [
        {
          text: `${result > 0 ? result : result * (-1)}`,
        }
      ],
      type: result < 0 ? '2' : '1'
    } as ExpressionTerm
    return resultTerm
  } else {
    console.log("含字母");
    
    console.log("前》", previous, "后》", next);
    // 此处算法需优化
    let previousConstant = 0 // 数字
    let previousCharConstantArray:ExpressionTerm = [] // 字母数组
    if (previous.constant) {
      previous.constant.forEach((subTerm: VariableTermElement) => {
        const waitConstant = subTerm.text ?? '0'
        if (stringContainChars(waitConstant)) {
          const charTerm = {
            text: waitConstant,
            type: subTerm.type
          } as ExpressionTerm
          previousCharConstantArray.push(charTerm)
        } else {
          previousConstant = previousConstant + Number(waitConstant) * (subTerm.type === '2' ? -1 : 1)
        }
      })
    } else {
      throw('进行加法运算的项，其参数类型错误！')
    }
    let nextConstant = 0 // 数字
    let nextCharConstantArray:ExpressionTerm = [] // 字母数组
    if (next.constant) {
      next.constant.forEach((subTerm: VariableTermElement) => {
        const waitConstant = subTerm.text ?? '0'
        if (stringContainChars(waitConstant)) {
          const charTerm = {
            text: waitConstant,
            type: subTerm.type
          } as ExpressionTerm
          nextCharConstantArray.push(charTerm)
        } else {
          nextConstant = nextConstant + Number(waitConstant) * (subTerm.type === '2' ? -1 : 1)
        }
      })
    } else {
      throw('进行加法运算的项，其参数类型错误！')
    }
    console.log(previousCharConstantArray, "》元素是字母》", nextCharConstantArray)

    let result = Number(previousConstant) * (previous.type === '2' ? -1 : 1) + Number(nextConstant) * (next.type === '2' ? -1 : 1)
    let resultTerm = {
      constant: [
        {
          text: `${result > 0 ? result : result * (-1)}`,
        }
      ],
      type: result < 0 ? '2' : '1'
    } as ExpressionTerm
    // 处理前一个
    if ((previous.type === '2' && result > 0) || (previous.type === '1' && result < 0)) {
      previousCharConstantArray.forEach((charTerm: ExpressionTerm) => {
        charTerm.type = charTerm.type === '1' ? '2' : '1'
      })
    }
    // 处理后一个
    if ((next.type === '2' && result > 0) || (next.type === '1' && result < 0)) {
      nextCharConstantArray.forEach((charTerm: ExpressionTerm) => {
        charTerm.type = charTerm.type === '1' ? '2' : '1'
      })
    }
    resultTerm.constant = resultTerm.constant?.concat(previousCharConstantArray).concat(nextCharConstantArray)
    return resultTerm
  }
}